/*******************************************************************************
* File Name: CSD_Touchpad_CSHL.h
* Version 2.40
*
* Description:
*  This file provides constants and parameter values for the High Level APIs
*  for CapSense CSD component.
*
* Note:
*
********************************************************************************
* Copyright 2013-2015, Cypress Semiconductor Corporation.  All rights reserved.
* You may use this file only in accordance with the license, terms, conditions,
* disclaimers, and limitations in the end user license agreement accompanying
* the software package with which this file was provided.
*******************************************************************************/

#if !defined(CY_CAPSENSE_CSD_CSHL_CSD_Touchpad_H)
#define CY_CAPSENSE_CSD_CSHL_CSD_Touchpad_H

#include "CSD_Touchpad.h"


/***************************************
*   Condition compilation parameters
***************************************/

#define CSD_Touchpad_SIGNAL_SIZE                    (8u)
#define CSD_Touchpad_AUTO_RESET                     (0u)
#define CSD_Touchpad_RAW_FILTER_MASK                (0u)

/* Signal size definition */
#define CSD_Touchpad_SIGNAL_SIZE_UINT8              (8u)
#define CSD_Touchpad_SIGNAL_SIZE_UINT16             (16u)

/* Auto reset definition */
#define CSD_Touchpad_AUTO_RESET_DISABLE             (0u)
#define CSD_Touchpad_AUTO_RESET_ENABLE              (1u)

/* Mask for RAW and POS filters */
#define CSD_Touchpad_MEDIAN_FILTER                  (0x01u)
#define CSD_Touchpad_AVERAGING_FILTER               (0x02u)
#define CSD_Touchpad_IIR2_FILTER                    (0x04u)
#define CSD_Touchpad_IIR4_FILTER                    (0x08u)
#define CSD_Touchpad_JITTER_FILTER                  (0x10u)
#define CSD_Touchpad_IIR8_FILTER                    (0x20u)
#define CSD_Touchpad_IIR16_FILTER                   (0x40u)
#define CSD_Touchpad_RAW_FILTERS_ENABLED            (0x01u)
#define CSD_Touchpad_RAW_FILTERS_DISABLED           (0x00u)

/***************************************
*           API Constants
***************************************/

/* Widgets constants definition */
#define CSD_Touchpad_TRACKPAD__TP        (0u)
#define CSD_Touchpad_TRACKPAD__TP_COL        (0u)
#define CSD_Touchpad_TRACKPAD__TP_ROW        (1u)

#define CSD_Touchpad_TOTAL_DIPLEXED_SLIDERS_COUNT        (0u)
#define CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT          (0u)
#define CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT          (0u)
#define CSD_Touchpad_TOTAL_TOUCH_PADS_COUNT              (1u)
#define CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT        (0u)
#define CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT       (1u)
#define CSD_Touchpad_TOTAL_BUTTONS_COUNT                 (0u)
#define CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT          (0u)
#define CSD_Touchpad_TOTAL_GENERICS_COUNT                (0u)

#define CSD_Touchpad_POS_FILTERS_MASK                    (0x19u)
#define CSD_Touchpad_LINEAR_SLIDERS_POS_FILTERS_MASK     (0x0u)
#define CSD_Touchpad_RADIAL_SLIDERS_POS_FILTERS_MASK     (0x0u)
#define CSD_Touchpad_TOUCH_PADS_POS_FILTERS_MASK         (0x0u)
#define CSD_Touchpad_TRACKPAD_GEST_POS_FILTERS_MASK      (0x19u)

#define CSD_Touchpad_UNUSED_DEBOUNCE_COUNTER_INDEX       (0u)

#define CSD_Touchpad_TOTAL_PROX_SENSORS_COUNT            (0u)

#define CSD_Touchpad_END_OF_SLIDERS_INDEX                (0u)
#define CSD_Touchpad_END_OF_TOUCH_PAD_INDEX              (1u)
#define CSD_Touchpad_END_OF_BUTTONS_INDEX                (1u)
#define CSD_Touchpad_END_OF_MATRIX_BUTTONS_INDEX         (1u)
#define CSD_Touchpad_END_OF_WIDGETS_INDEX                (2u)



#define CSD_Touchpad_TOTAL_SLIDERS_COUNT            ( CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT + \
                                                          CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT )

#define CSD_Touchpad_TOTAL_CENTROIDS_COUNT          ( CSD_Touchpad_TOTAL_SLIDERS_COUNT + \
                                                         (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT * 2u) +\
                                                         (CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT * 4u))

#define CSD_Touchpad_TOTAL_CENTROIDS_BASIC_COUNT    ( CSD_Touchpad_TOTAL_SLIDERS_COUNT + \
                                                         (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT * 2u))

#define CSD_Touchpad_TOTAL_CENTROID_AXES_COUNT      ( CSD_Touchpad_TOTAL_SLIDERS_COUNT + \
                                                         (CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT * 2u) +\
                                                         (CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT * 2u))

#define CSD_Touchpad_TOTAL_WIDGET_COUNT             ( CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT + \
                                                          CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT + \
                                                          CSD_Touchpad_TOTAL_TOUCH_PADS_COUNT + \
                                                          CSD_Touchpad_TOTAL_BUTTONS_COUNT + \
                                                          CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT )

#define CSD_Touchpad_ANY_POS_FILTER                 ( CSD_Touchpad_MEDIAN_FILTER | \
                                                          CSD_Touchpad_AVERAGING_FILTER | \
                                                          CSD_Touchpad_IIR2_FILTER | \
                                                          CSD_Touchpad_IIR4_FILTER | \
                                                          CSD_Touchpad_JITTER_FILTER )

#define CSD_Touchpad_IS_DIPLEX_SLIDER               ( CSD_Touchpad_TOTAL_DIPLEXED_SLIDERS_COUNT > 0u)

#define CSD_Touchpad_IS_NON_DIPLEX_SLIDER           ( (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT - \
                                                           CSD_Touchpad_TOTAL_DIPLEXED_SLIDERS_COUNT) > 0u)
#define CSD_Touchpad_ADD_SLIDER_TYPE                ((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) ? \
                                                        ((CSD_Touchpad_TOTAL_TOUCH_PADS_COUNT > 0u) || \
                                                         (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u)) : 0u)

#define CSD_Touchpad_TOTAL_PROX_SENSOR_COUNT        (CSD_Touchpad_TOTAL_PROX_SENSORS_COUNT)

#define CSD_Touchpad_WIDGETS_TBL_SIZE               ( CSD_Touchpad_TOTAL_WIDGET_COUNT + \
                                                          CSD_Touchpad_TOTAL_GENERICS_COUNT)

#define CSD_Touchpad_WIDGET_PARAM_TBL_SIZE          (CSD_Touchpad_TOTAL_BUTTONS_COUNT + \
                                                         CSD_Touchpad_TOTAL_SLIDERS_COUNT +\
                                                         CSD_Touchpad_TOTAL_TOUCH_PADS_BASIC_COUNT * 2u + \
                                                         CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT * 2u +\
                                                         CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT * 2u)

#define CSD_Touchpad_THRESHOLD_TBL_SIZE         (CSD_Touchpad_WIDGET_PARAM_TBL_SIZE)
#define CSD_Touchpad_DEBOUNCE_CNT_TBL_SIZE      (CSD_Touchpad_WIDGET_PARAM_TBL_SIZE)
#define CSD_Touchpad_RAW_DATA_INDEX_TBL_SIZE    (CSD_Touchpad_WIDGET_PARAM_TBL_SIZE +\
                                                     CSD_Touchpad_TOTAL_GENERICS_COUNT)

#define CSD_Touchpad_RES_MULT                   (256u)


#define CSD_Touchpad_NOT_WIDGET                     (0xFFFFFFFFu)

/*Types of centroids */
#define CSD_Touchpad_TYPE_RADIAL_SLIDER             (0x01u)
#define CSD_Touchpad_TYPE_LINEAR_SLIDER             (0x02u)
#define CSD_Touchpad_TYPE_GENERIC                   (0xFFu)

/* Defines if sensors or widgets are active */
#define CSD_Touchpad_SENSOR_IS_ACTIVE               (0x01u)
#define CSD_Touchpad_SENSOR_1_IS_ACTIVE             (0x01u)
#define CSD_Touchpad_SENSOR_2_IS_ACTIVE             (0x02u)
#define CSD_Touchpad_WIDGET_IS_ACTIVE               (0x01u)

/* Defines diplex type of Slider */
#define CSD_Touchpad_IS_DIPLEX                      (0x80u)

/* Defines fingers positions on Slider  */
#define CSD_Touchpad_POS_PREV                       (0u)
#define CSD_Touchpad_POS                            (1u)
#define CSD_Touchpad_POS_NEXT                       (2u)
#define CSD_Touchpad_CENTROID_ROUND_VALUE           (0x7F00u)
#define CSD_Touchpad_MAXIMUM_CENTROID               (0xFFu)

#define CSD_Touchpad_NEGATIVE_NOISE_THRESHOLD       (20u)
#define CSD_Touchpad_LOW_BASELINE_RESET             (5u)


/***************************************
*        Function Prototypes
***************************************/

void CSD_Touchpad_InitializeSensorBaseline(uint32 sensor);
void CSD_Touchpad_InitializeAllBaselines(void);
void CSD_Touchpad_InitializeEnabledBaselines(void);
void CSD_Touchpad_UpdateSensorBaseline(uint32 sensor);
void CSD_Touchpad_UpdateBaselineNoThreshold(uint32 sensor);
void CSD_Touchpad_UpdateEnabledBaselines(void);
void CSD_Touchpad_UpdateWidgetBaseline(uint32 widget);
uint16 CSD_Touchpad_GetBaselineData(uint32 sensor);
void CSD_Touchpad_SetBaselineData(uint32 sensor, uint16 data);
void CSD_Touchpad_BaseInit(uint32 sensor);

#if (CSD_Touchpad_IS_DIPLEX_SLIDER)
    uint8 CSD_Touchpad_FindMaximum(uint8 offset, uint8 count, uint8 fingerThreshold, const uint8 *diplex);
#else
    uint8 CSD_Touchpad_FindMaximum(uint8 offset, uint8 count, uint8 fingerThreshold);
#endif /* (CSD_Touchpad_IS_DIPLEX_SLIDER) */

#if (CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT > 0u)
    uint8 CSD_Touchpad_CalcCentroid(uint8 maximum, uint8 offset, \
                                    uint8 count, uint32 resolution, uint8 noiseThreshold);
#else
    uint8 CSD_Touchpad_CalcCentroid(uint8 maximum, uint8 offset, \
                                    uint8 count, uint16 resolution, uint8 noiseThreshold);
#endif /* (CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT > 0u) */


uint8 CSD_Touchpad_GetFingerThreshold(uint32 widget);
uint8 CSD_Touchpad_GetNoiseThreshold(uint32 widget);
uint8 CSD_Touchpad_GetFingerHysteresis(uint32 widget);
uint8 CSD_Touchpad_GetNegativeNoiseThreshold(uint32 widget);

#if(CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE)
    void CSD_Touchpad_SetFingerThreshold(uint32 widget, uint8 value);
    void CSD_Touchpad_SetNoiseThreshold(uint32 widget, uint8 value);
    void CSD_Touchpad_SetFingerHysteresis(uint32 widget, uint8 value);
    void CSD_Touchpad_SetNegativeNoiseThreshold(uint32 widget, uint8 value);
    void CSD_Touchpad_SetDebounce(uint32 widget, uint8 value);
    void CSD_Touchpad_SetLowBaselineReset(uint32 sensor, uint8 value);
#endif /* (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE) */

uint8 CSD_Touchpad_GetDiffCountData(uint32 sensor);
void CSD_Touchpad_SetDiffCountData(uint32 sensor, uint8 value);

uint32 CSD_Touchpad_CheckIsSensorActive(uint32 sensor);
uint32 CSD_Touchpad_CheckIsWidgetActive(uint32 widget);
uint32 CSD_Touchpad_CheckIsAnyWidgetActive(void);
void CSD_Touchpad_EnableWidget(uint32 widget);
void CSD_Touchpad_DisableWidget(uint32 widget);
void CSD_Touchpad_EnableRawDataFilters(void);
void CSD_Touchpad_DisableRawDataFilters(void);

#if (CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT)
    uint32 CSD_Touchpad_GetMatrixButtonPos(uint32 widget, uint8* pos);
#endif /* (CSD_Touchpad_TOTAL_MATRIX_BUTTONS_COUNT) */

#if((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u))
    uint16 CSD_Touchpad_GetCentroidPos(uint32 widget);
#endif /* ((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u)) */
#if((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u))
    uint16 CSD_Touchpad_GetRadialCentroidPos(uint32 widget);
#endif /* #if((CSD_Touchpad_TOTAL_RADIAL_SLIDERS_COUNT > 0u) || (CSD_Touchpad_TOTAL_LINEAR_SLIDERS_COUNT > 0u)) */
#if (CSD_Touchpad_TOTAL_TOUCH_PADS_COUNT)
    uint32 CSD_Touchpad_GetTouchCentroidPos(uint32 widget, uint16* pos);
#endif /* (CSD_Touchpad_TOTAL_TOUCH_PADS_COUNT) */

uint32 CSD_Touchpad_GetWidgetNumber(uint32 sensor);
uint8 CSD_Touchpad_GetLowBaselineReset(uint32 sensor);
uint8 CSD_Touchpad_GetDebounce(uint32 widget);

/* Filter function prototypes for High level APIs */

/* Median filter function prototype */
#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      ((CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO)) )
    uint16 CSD_Touchpad_MedianFilter(uint16 x1, uint16 x2, uint16 x3);
#endif /* ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_MEDIAN_FILTER)) || \
      ((CSD_Touchpad_TUNING_METHOD == CSD_Touchpad__TUNING_AUTO)) ) */

/* Averaging filter function prototype */
#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_AVERAGING_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_AVERAGING_FILTER)) )
    uint16 CSD_Touchpad_AveragingFilter(uint16 x1, uint16 x2, uint16 x3);
#endif /* CSD_Touchpad_RAW_FILTER_MASK && CSD_Touchpad_POS_FILTERS_MASK */

/* IIR2Filter(1/2prev + 1/2cur) filter function prototype */
#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR2_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_IIR2_FILTER)) )
    uint16 CSD_Touchpad_IIR2Filter(uint16 x1, uint16 x2);
#endif /* CSD_Touchpad_RAW_FILTER_MASK && CSD_Touchpad_POS_FILTERS_MASK */

/* IIR4Filter(3/4prev + 1/4cur) filter function prototype */
#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR4_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_IIR4_FILTER)) )
    uint16 CSD_Touchpad_IIR4Filter(uint16 x1, uint16 x2);
#endif /* CSD_Touchpad_RAW_FILTER_MASK && CSD_Touchpad_POS_FILTERS_MASK */

/* IIR8Filter(7/8prev + 1/8cur) filter function prototype - RawCounts only */
#if (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR8_FILTER))
    uint16 CSD_Touchpad_IIR8Filter(uint16 x1, uint16 x2);
#endif /* CSD_Touchpad_RAW_FILTER_MASK */

/* IIR16Filter(15/16prev + 1/16cur) filter function prototype - RawCounts only */
#if (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_IIR16_FILTER))
    uint16 CSD_Touchpad_IIR16Filter(uint16 x1, uint16 x2);
#endif /* CSD_Touchpad_RAW_FILTER_MASK */

/* JitterFilter filter function prototype */
#if ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_JITTER_FILTER)) || \
      (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_JITTER_FILTER)) || \
      (0u != (CSD_Touchpad_TRACKPAD_GEST_POS_FILTERS_MASK & CSD_Touchpad_JITTER_FILTER)))
    uint16 CSD_Touchpad_JitterFilter(uint16 x1, uint16 x2);
#endif /* ( (0u != (CSD_Touchpad_RAW_FILTER_MASK & CSD_Touchpad_JITTER_FILTER)) || \
       *    (0u != (CSD_Touchpad_POS_FILTERS_MASK & CSD_Touchpad_JITTER_FILTER)) )
       *    (0u != (CSD_Touchpad_TRACKPAD_GEST_POS_FILTERS_MASK & CSD_Touchpad_JITTER_FILTER)) )
       */


/***************************************
*     Vars with External Linkage
***************************************/
extern uint16 CSD_Touchpad_sensorBaseline[CSD_Touchpad_TOTAL_SENSOR_COUNT];
extern uint8  CSD_Touchpad_sensorBaselineLow[CSD_Touchpad_TOTAL_SENSOR_COUNT];
extern uint8 CSD_Touchpad_sensorSignal[CSD_Touchpad_TOTAL_SENSOR_COUNT];
extern uint8  CSD_Touchpad_sensorOnMask[CSD_Touchpad_TOTAL_SENSOR_MASK];

extern uint8 CSD_Touchpad_lowBaselineResetCnt[CSD_Touchpad_TOTAL_SENSOR_COUNT];
extern uint8 CSD_Touchpad_lowBaselineReset[CSD_Touchpad_TOTAL_SENSOR_COUNT];

/* Generated by Customizer */
#if (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE)
    extern uint8 CSD_Touchpad_fingerThreshold[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
    extern uint8 CSD_Touchpad_noiseThreshold[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
    extern uint8 CSD_Touchpad_negativeNoiseThreshold[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
    extern uint8 CSD_Touchpad_hysteresis[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
    extern uint8  CSD_Touchpad_debounce[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
#else
    extern const uint8 CSD_Touchpad_fingerThreshold[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
    extern const uint8 CSD_Touchpad_noiseThreshold[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
    extern const uint8 CSD_Touchpad_hysteresis[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
    extern const uint8  CSD_Touchpad_debounce[CSD_Touchpad_WIDGET_PARAM_TBL_SIZE];
#endif /* (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE) */

extern const uint8 CSD_Touchpad_rawDataIndex[CSD_Touchpad_RAW_DATA_INDEX_TBL_SIZE];
extern const uint8 CSD_Touchpad_numberOfSensors[CSD_Touchpad_RAW_DATA_INDEX_TBL_SIZE];

#if (0u != CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT)
    #if (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE)
        extern uint8 CSD_Touchpad_posFiltersMask[CSD_Touchpad_TOTAL_CENTROID_AXES_COUNT];
    #else
        extern const uint8 CSD_Touchpad_posFiltersMask[CSD_Touchpad_TOTAL_CENTROID_AXES_COUNT];
    #endif /* (CSD_Touchpad_TUNING_METHOD != CSD_Touchpad__TUNING_NONE) */

    extern uint32 CSD_Touchpad_centroidMult[CSD_Touchpad_TOTAL_CENTROID_AXES_COUNT];
#endif /* (0u != CSD_Touchpad_TOTAL_TRACKPAD_GESTURES_COUNT) */

/***************************************
*        Obsolete Names
***************************************/
#define CSD_Touchpad_SensorRaw              CSD_Touchpad_sensorRaw
#define CSD_Touchpad_SensorEnableMask       CSD_Touchpad_sensorEnableMask
#define CSD_Touchpad_SensorBaseline         CSD_Touchpad_sensorBaseline
#define CSD_Touchpad_SensorBaselineLow      CSD_Touchpad_sensorBaselineLow
#define CSD_Touchpad_SensorSignal           CSD_Touchpad_sensorSignal
#define CSD_Touchpad_SensorOnMask           CSD_Touchpad_sensorOnMask
#define CSD_Touchpad_LowBaselineResetCnt    CSD_Touchpad_lowBaselineResetCnt

#endif /* CY_CAPSENSE_CSD_CSHL_CSD_Touchpad_H */

/* [] END OF FILE */
